package com.zfb.videoscript

import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.viewModels
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.material3.*
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import androidx.compose.ui.window.Dialog
import com.zfb.videoscript.ui.theme.ZFBVideoScriptTheme
import com.zfb.videoscript.viewmodel.MainViewModel

class MainActivity : ComponentActivity() {
    private val viewModel: MainViewModel by viewModels()
    
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            ZFBVideoScriptTheme {
                Surface(
                    modifier = Modifier.fillMaxSize(),
                    color = MaterialTheme.colorScheme.background
                ) {
                    MainScreen(viewModel)
                }
            }
        }
    }
}

@Composable
fun MainScreen(viewModel: MainViewModel) {
    var speed by remember { mutableStateOf("800") }
    var startDelay by remember { mutableStateOf("21") }
    var endDelay by remember { mutableStateOf("30") }
    var likeRate by remember { mutableStateOf("66") }
    var isCheckRedPacket by remember { mutableStateOf(true) }
    var selectedPlatform by remember { mutableStateOf(0) }
    var showLogs by remember { mutableStateOf(false) }
    
    val isRunning by viewModel.isRunning.collectAsState()
    val logs by viewModel.logs.collectAsState()
    val updateInfo by viewModel.updateInfo.collectAsState()
    val updateProgress by viewModel.updateProgress.collectAsState()

    // 更新对话框
    updateInfo?.let { (newVersion, info) ->
        Dialog(onDismissRequest = {}) {
            Surface(
                modifier = Modifier
                    .fillMaxWidth()
                    .padding(16.dp),
                shape = MaterialTheme.shapes.medium,
                color = MaterialTheme.colorScheme.surface
            ) {
                Column(
                    modifier = Modifier.padding(16.dp)
                ) {
                    Text(
                        text = "发现新版本：$newVersion",
                        style = MaterialTheme.typography.titleLarge
                    )
                    Spacer(modifier = Modifier.height(8.dp))
                    Text(
                        text = info,
                        style = MaterialTheme.typography.bodyMedium
                    )
                    Spacer(modifier = Modifier.height(16.dp))
                    
                    if (updateProgress > 0) {
                        LinearProgressIndicator(
                            progress = updateProgress / 100f,
                            modifier = Modifier.fillMaxWidth()
                        )
                        Text(
                            text = "下载进度：$updateProgress%",
                            modifier = Modifier.align(Alignment.CenterHorizontally)
                        )
                    } else {
                        Row(
                            modifier = Modifier.fillMaxWidth(),
                            horizontalArrangement = Arrangement.End
                        ) {
                            TextButton(
                                onClick = { viewModel.downloadUpdate("https://example.com/update.apk") }
                            ) {
                                Text("立即更新")
                            }
                        }
                    }
                }
            }
        }
    }

    Column(
        modifier = Modifier
            .fillMaxSize()
            .padding(16.dp)
    ) {
        Text(
            text = "ZFB刷视频",
            style = MaterialTheme.typography.headlineMedium,
            modifier = Modifier.padding(bottom = 16.dp)
        )

        if (!showLogs) {
            // 配置界面
            OutlinedTextField(
                value = speed,
                onValueChange = { speed = it },
                label = { Text("滑动速度 (毫秒)") },
                modifier = Modifier
                    .fillMaxWidth()
                    .padding(vertical = 8.dp)
            )

            Row(
                modifier = Modifier
                    .fillMaxWidth()
                    .padding(vertical = 8.dp),
                horizontalArrangement = Arrangement.SpaceBetween
            ) {
                OutlinedTextField(
                    value = startDelay,
                    onValueChange = { startDelay = it },
                    label = { Text("开始延迟 (秒)") },
                    modifier = Modifier
                        .weight(1f)
                        .padding(end = 8.dp)
                )
                OutlinedTextField(
                    value = endDelay,
                    onValueChange = { endDelay = it },
                    label = { Text("结束延迟 (秒)") },
                    modifier = Modifier
                        .weight(1f)
                        .padding(start = 8.dp)
                )
            }

            OutlinedTextField(
                value = likeRate,
                onValueChange = { likeRate = it },
                label = { Text("点赞概率 (%)") },
                modifier = Modifier
                    .fillMaxWidth()
                    .padding(vertical = 8.dp)
            )

            Row(
                modifier = Modifier
                    .fillMaxWidth()
                    .padding(vertical = 8.dp)
            ) {
                Checkbox(
                    checked = isCheckRedPacket,
                    onCheckedChange = { isCheckRedPacket = it }
                )
                Text(
                    text = "检测到去领取红包停止脚本",
                    modifier = Modifier.padding(start = 8.dp)
                )
            }

            Row(
                modifier = Modifier
                    .fillMaxWidth()
                    .padding(vertical = 8.dp)
            ) {
                RadioButton(
                    selected = selectedPlatform == 0,
                    onClick = { selectedPlatform = 0 }
                )
                Text(
                    text = "支付宝刷视频",
                    modifier = Modifier.padding(start = 8.dp, end = 16.dp)
                )
                RadioButton(
                    selected = selectedPlatform == 1,
                    onClick = { selectedPlatform = 1 }
                )
                Text(
                    text = "拼多多刷视频",
                    modifier = Modifier.padding(start = 8.dp)
                )
            }
        } else {
            // 日志界面
            LazyColumn(
                modifier = Modifier
                    .weight(1f)
                    .fillMaxWidth()
            ) {
                items(logs) { log ->
                    Text(
                        text = log,
                        modifier = Modifier
                            .fillMaxWidth()
                            .padding(vertical = 4.dp)
                    )
                }
            }
        }

        Row(
            modifier = Modifier
                .fillMaxWidth()
                .padding(vertical = 16.dp),
            horizontalArrangement = Arrangement.SpaceBetween
        ) {
            Button(
                onClick = {
                    if (isRunning) {
                        viewModel.stopScript()
                    } else {
                        viewModel.startScript(
                            speed = speed.toIntOrNull() ?: 800,
                            startDelay = startDelay.toIntOrNull() ?: 21,
                            endDelay = endDelay.toIntOrNull() ?: 30,
                            likeRate = likeRate.toIntOrNull() ?: 66,
                            checkRedPacket = isCheckRedPacket,
                            isPDD = selectedPlatform == 1
                        )
                    }
                },
                modifier = Modifier.weight(1f)
            ) {
                Text(if (isRunning) "停止运行" else "开始运行")
            }
            
            Button(
                onClick = { showLogs = !showLogs },
                modifier = Modifier
                    .padding(start = 8.dp)
            ) {
                Text(if (showLogs) "显示配置" else "显示日志")
            }
        }
    }
} 